package org.ucas.faker.web.fetcher.cond;

import javafx.util.Pair;
import org.ucas.faker.web.fetcher.meta.ColumnField;
import org.ucas.faker.web.fetcher.meta.Table;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;

public class AndCond extends FetchCond implements Serializable {

  public Set<FetchCond> conds = new HashSet<>();

  public AndCond() {
  }

  public AndCond(List<Pair<ColumnField,ColumnField>> constraints) {
    for(Pair<ColumnField,ColumnField> constraint:constraints) {
      this.add(new CompCond(constraint.getKey(),"=",constraint.getValue()));
    }
  }

  @Override
  public String sql() {
    if(conds == null || conds.size() == 0) return "";
    return conds.stream().map(FetchCond::sql).collect(Collectors.joining(" AND "));
  }

  public void add(FetchCond cond) {
    conds.add(cond);
  }

  @Override
  public FetchCond and(FetchCond cond) {
    if(cond!=null) conds.add(cond);
    return this;
  }

  @Override
  public void replace(Table src, Table dst) {
    for(FetchCond cond:conds) cond.replace(src,dst);
  }
}
